﻿
Imports command = centrosphere.job.simple.command

Public Class simpleJobRcEmitter
    Inherits rc_pac_emitter

    Public Sub New(ByVal host As String, ByVal port As Int32,
                   Optional ByVal holdtimeMs As Int64 = tcpTalk4.defaultHoldTimeMs,
                   Optional ByVal livetimeMs As Int64 = tcpTalk4.defaultLiveTimeMs)
        MyBase.New(host, port, holdtimeMs, livetimeMs)
    End Sub

    Private Overloads Function runjob(ByVal cmd As command,
                                      ByVal i As Int64,
                                      ByVal o As pointer(Of Int64),
                                      ByRef ec As eventComb) As Boolean
        Dim job As simpleJob = Nothing
        job = New simpleJob()
        job.cmd = New bytesByte(cmd)
        job.i64 = New bytesInt64(i)
        Dim ec2 As eventComb = Nothing
        ec = New eventComb(Function() As Boolean
                               Return eva(ec2, runjob(job)) AndAlso
                                      waitfor(ec2) AndAlso
                                      goto_next()
                           End Function,
                           Function() As Boolean
                               If ec2.end_result() Then
                                   eva(o, +(job.i64))
                                   Return goto_end()
                               Else
                                   Return False
                               End If
                           End Function)
        Return True
    End Function

    Public Function increment(ByVal i As Int64, ByVal o As pointer(Of Int64), ByRef ec As eventComb) As Boolean
        Return runjob(command.increment, i, o, ec)
    End Function

    Public Function decrement(ByVal i As Int64, ByVal o As pointer(Of Int64), ByRef ec As eventComb) As Boolean
        Return runjob(command.decrement, i, o, ec)
    End Function
End Class
